{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3df7fa97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            报告期         *负债合计  *归属于母公司所有者权益合计\n",
      "112  2024-12-31  5.270000e+12    4.948420e+11\n",
      "113  2025-03-31  5.270000e+12    5.061120e+11\n",
      "报告期: 2025-03-31\n",
      "负债率: 91.24%\n",
      "负债率环比变化: -0.18%\n",
      "负债环比变化: 0.00\n",
      "净资产环比变化: 112.70 亿\n",
      "健康评分: 1/2\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "stock_code = \"000001\"\n",
    "\n",
    "def parse_amount(x):\n",
    "    if pd.isna(x):\n",
    "        return 0\n",
    "    if isinstance(x, (int, float)):\n",
    "        return x\n",
    "    x = str(x).replace(\",\", \"\").strip()\n",
    "    sign = -1 if x.startswith(\"-\") else 1\n",
    "    x = x.lstrip(\"+-\")\n",
    "\n",
    "    # 支持万、亿、万亿\n",
    "    if x.endswith(\"万亿\"):\n",
    "        return sign * float(x[:-2]) * 1e12\n",
    "    elif x.endswith(\"亿\"):\n",
    "        return sign * float(x[:-1]) * 1e8\n",
    "    elif x.endswith(\"万\"):\n",
    "        return sign * float(x[:-1]) * 1e4\n",
    "    else:\n",
    "        return sign * float(x)\n",
    "\n",
    "# ================================\n",
    "# 金额格式化显示\n",
    "# ================================\n",
    "def format_amount(val):\n",
    "    if val is None:\n",
    "        return \"\"\n",
    "    abs_val = abs(val)\n",
    "    if abs_val >= 1e12:\n",
    "        return f\"{val / 1e12:.2f} 万亿\"\n",
    "    elif abs_val >= 1e8:\n",
    "        return f\"{val / 1e8:.2f} 亿\"\n",
    "    elif abs_val >= 1e4:\n",
    "        return f\"{val / 1e4:.2f} 万\"\n",
    "    else:\n",
    "        return f\"{val:.2f}\"\n",
    "\n",
    "# ================================\n",
    "# 获取按年度数据\n",
    "# ================================\n",
    "df = ak.stock_financial_debt_ths(symbol=stock_code, indicator=\"按报告期\")\n",
    "df = df[[\"报告期\", \"*负债合计\", \"*归属于母公司所有者权益合计\"]].copy()\n",
    "for col in [\"*负债合计\", \"*归属于母公司所有者权益合计\"]:\n",
    "    df[col] = df[col].apply(parse_amount)\n",
    "\n",
    "df = df.sort_values(\"报告期\").reset_index(drop=True)\n",
    "print(df.tail(2))\n",
    "\n",
    "# ================================\n",
    "# 计算负债率及环比变化\n",
    "# ================================\n",
    "latest = df.iloc[-1]\n",
    "prev = df.iloc[-2]\n",
    "\n",
    "debt_ratio = latest[\"*负债合计\"] / (latest[\"*负债合计\"] + latest[\"*归属于母公司所有者权益合计\"]) * 100\n",
    "debt_ratio_prev = prev[\"*负债合计\"] / (prev[\"*负债合计\"] + prev[\"*归属于母公司所有者权益合计\"]) * 100\n",
    "debt_ratio_hb_change = debt_ratio - debt_ratio_prev\n",
    "\n",
    "debt_hb_change = latest[\"*负债合计\"] - prev[\"*负债合计\"]\n",
    "equity_hb_change = latest[\"*归属于母公司所有者权益合计\"] - prev[\"*归属于母公司所有者权益合计\"]\n",
    "\n",
    "# ================================\n",
    "# 健康评分（总分 2 分）\n",
    "# ================================\n",
    "score = 0\n",
    "if debt_ratio < 50:       # 负债率 <50 得 1 分\n",
    "    score += 1\n",
    "if debt_ratio_hb_change < 0:  # 负债率减少 再得 1 分\n",
    "    score += 1\n",
    "\n",
    "# ================================\n",
    "# 打印结果\n",
    "# ================================\n",
    "print(f\"报告期: {latest['报告期']}\")\n",
    "print(f\"负债率: {debt_ratio:.2f}%\")\n",
    "print(f\"负债率环比变化: {debt_ratio_hb_change:.2f}%\")\n",
    "print(f\"负债环比变化: {format_amount(debt_hb_change)}\")\n",
    "print(f\"净资产环比变化: {format_amount(equity_hb_change)}\")\n",
    "print(f\"健康评分: {score}/2\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
